package www.alterzhang.cn.service.impl;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import www.alterzhang.cn.service.AsyncService;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

/**
 * @author dycong
 * @date 2019/7/15 15:38
 */
@Service
public class AsyncServiceImpl implements AsyncService {


    @Override
    @Async
    public void get0() {

    }

    @Async
    @Override
    public Integer get2() throws InterruptedException {
        System.out.println("======get2 start======");
        Thread.sleep(2000);
        System.out.println("======get2 end======" + Thread.currentThread());
        return 2;
    }

    @Override
    @Async
    public ListenableFuture get3() throws InterruptedException {
        System.out.println("======get3 start======");
        Thread.sleep(2000);
        System.out.println("======get3 end======" + Thread.currentThread());
        return new AsyncResult<Integer>(3);
    }

    @Async
    @Override
    public CompletableFuture get4() throws InterruptedException {
        System.out.println("======get4 start======");
        Thread.sleep(2000);
        System.out.println("======get4 end======" + Thread.currentThread());
        return CompletableFuture.supplyAsync(() -> 4);
    }

    @Async
    @Override
    public Future get5() throws InterruptedException {
        System.out.println("======get5 start======");
        Thread.sleep(2000);
        System.out.println("======get5 end======" + Thread.currentThread());
        return new AsyncResult<Integer>(5);
    }

    // TODO: 2019/7/15 没有异步执行，没有开启异步（SimpleAsyncTaskExecutor），仍用的当前线程执行的（http-nio-8080-exec-1,5,main）
    @Override
    public Integer get6() throws InterruptedException {
        System.out.println("======get6 start======");
        Thread.sleep(2000);
        System.out.println("======get6 end======" + Thread.currentThread());
        return 6;
    }


}
